<?php
	class companiacarpetas extends Controller {
		public function __construct() {
			parent::Controller();
			$this->load->model(array('mcarpetastatus','Mcompaniacarpetas','usuarios','Mapellido','Mactividad'));
			
		}

		function index() {
			$this->load->view('main', array('content_page' => 'companiacarpetas_list','menutop' => 0));
		}
		
		function add(){
			
			$apellido = $this->Mapellido->loadApellido();
			$status = $this->mcarpetastatus->LoadStatusDocumento();
			$this->load->view('main', array('content_page' => 'companiacarpetas_add','menutop' => 0,'status'=>$status,'apellido'=>$apellido));
		}
		
		function save(){

			$fecha = explode('/',$this->input->post('date'));
			$fecha_real = $fecha[2].'-'.$fecha[0].'-'.$fecha[1].' '.date('H:i:s');
			
            $nombre = strtoupper($this->input->post('name'));
			$data = array(
			               'Referencia'=>$nombre,
			               'IdUsuario'=>$this->session->userdata('LOGGED_USR_ID'),
			               'FechaInicio'=>$fecha_real,
			               'IdStatus'=>$this->input->post('status'),
			               'type'=>1,
			               'IdApellido'=>$this->input->post('apellido'),
			               'Id_Idioma'=>$this->input->post('idioma')
			             );
		
			             $this->Mcompaniacarpetas->add($data);	
			              	 
		    redirect('companiacarpetas');	              
		}
		
		function save2(){
			
			$fecha = explode('/',$this->input->post('date'));
			$fecha_real = $fecha[2].'-'.$fecha[0].'-'.$fecha[1].' '.date('H:i:s');
			
			$nombre = strtoupper($this->input->post('name'));
			
			$data = array(
			               'Referencia'=>$nombre,
			               'IdUsuario'=>$this->session->userdata('LOGGED_USR_ID'),
			               'FechaInicio'=>$fecha_real,
			               'IdStatus'=>$this->input->post('status'),
			               'IdApellido'=>$this->input->post('apellido'),
			               'Id_Idioma'=>$this->input->post('idioma')
			             );
		
			             $this->Mcompaniacarpetas->edit($data,$this->input->post('id'));	
			              	 
		    redirect('companiacarpetas');	              
		}
		
		
		function edit($id){
			$apellido = $this->Mapellido->loadApellido();
			$status = $this->mcarpetastatus->LoadStatusDocumento();
			$iddocument = $this->Mcompaniacarpetas->LoadEtapaById($id);
	
			$this->load->view('main', array('content_page' => 'companiacapetas_edit','menutop' => 0,'status'=>$status,'document'=>$iddocument, 'apellido'=>$apellido));
		}
		
		function delete(){
			 $id = $this->input->post('delete_rows');
		     $this->Mcompaniacarpetas->delete($id);
		     
		}
	
		function data() {
			$page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
			$sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'id'; // Columna para el order by
			$sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'asc'; // Orden (asc|desc)
			$limit = $this->input->post('rows') ? $this->input->post('rows') : 30;
			$start = $limit * $page - $limit;
			if($start < 0) $start = 0;

			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
			header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
			header("Cache-Control: no-cache, must-revalidate" );
			header("Pragma: no-cache" );

			$records = $this->Mcompaniacarpetas->LoadStatusDocumento($start, $limit, $sortname, $sortorder);

			$totalRecords = count($records);

			$totalPages = ceil($totalRecords/$limit);
			if ($page > $totalPages) $page = $totalPages;

			$json_output = array(
				'page'		=> $page,
				'total'		=> $totalPages,
				'records'	=> $totalRecords,
				'rows'		=> array()
			);

			if($records != false) {
				foreach($records as $estatus) {
				          
					    $status2 = $this->mcarpetastatus->LoadEtapaById($estatus->getIdstatus());
					    $usuario = $this->usuarios->LoadUsuarioById($estatus->getIdUsuario());
					    $apellido = $this->Mapellido->LoadApellidoById($estatus->getApellido());
					    $actividad = $this->Mactividad->LoadActividadById($apellido->getIdActividad());
					      
					     if ($estatus->getType() == 1) {
					     	$juridicon = 'Company24h';
					     }else{
					     	$juridicon = 'Company24h';
					     }
					    
					      $arr = $json_output['rows'][] = array(
					                                            'iddocumento'    => $estatus->getId(), 	
					                                            'referencia'     => $estatus->getReferenccia(), 
					                                            'usuario'        => $usuario->getUsernameUsuario(),
					                                            'date'           => substr($estatus->getDate(),0,10), 	
					                                            'status'         => $status2->getDescriccion(),
					                                            'type'           => $juridicon,
					                                            'apellido'       =>$apellido->getDescripcionApellido(),
					                                            'actividad'      =>$actividad->getDescripcionActividad()
					                                            ); 
					                                            
				}
			}

			echo json_encode($json_output);
		}
	}